const BehaviorAll = require("../../../behaviors/behavior_all.js");
const BehaviorAddress = require("../../../template/address/behavior.js");
const ServiceOrder = require("../../../service/service_order.js");
const ServiceScore = require("../../../service/service_score.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll, BehaviorAddress],
  /**
   * 页面的初始数据
   */
  data: {
    allScore:0,
    /**
      数据格式
        {
          id:1, number:'a', state:xxx, stateComment:'xxx', totalScore:100.0, amount:3,
          items:[
            {id:'{}', name:'xxx', number:'yyy', score:10.0, imgUrl:''}
          ]
        }
     */
    order: null,
    orderComment: null,
    orderId: null,
    /**
     * 购物车相关
     */
    car_amount: 0,
    triggerRetrieveData: false
  },

  methods:{
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      const orderId = options.id;
      _this.setData({ 
        orderId,
        /* 地址配置 */
        b_address_addressExistDefaultHideAddShowFlag: true,
        b_address_addressInfoShowFlag: true,
        b_address_addressInfoModifyShowFlag: true,
        b_address_addressInfoListShowFlag: true,
        //
        b_address_addressServiceCreateFlag: false,
        b_address_addressServiceModifyFlag: false,
        b_address_addressServiceDeleteFlag: false,
        //
        b_address_addressListSelectShowFlag: true,
        b_address_addressListEditShowFlag: false,
        b_address_addressListDeleteShowFlag: false
      })
      // 加载数据
      ServiceScore.find(function (data) {
        _this.setData({
          allScore: data
        })  
      })
      ServiceOrder.findGradeById(orderId, function (data) {
        _this.setData({
          order: data,
          // b_address_addressData: { 
          //   id: null, 
          //   username: data.address.username, 
          //   phone: data.address.phone, 
          //   detailAddress: data.address.address, 
          //   area: data.address.fixAddress, 
          //   townId: data.address.townId, 
          //   defaultFlag: false
          // }
        })
        
      })
    },
    /**
    * onShow
    */
    onShow: function (e) {
      const _this = this;
      _this.setData({ triggerRetrieveData: true });
    },
    /**
     * 修改订单备注
     */
    onChangeOrderComment: function(e){
      const _this = this;
      const comment = e.detail.value;
      _this.setData({ orderComment: comment });
    },
    /**
     * 提交订单
     */
    onTriggerSubmit: function (e) {
      const _this = this;
      const address = _this.data.b_address_addressData;
      const order = _this.data.order;
      // 验证地址
      if (!_this.b_address_validateAddressData(e)) {
        return;
      }
      // 提交
      ServiceOrder.gradeOrderPay(order.id, {address, comment: _this.data.orderComment}, function (data) {
        wx.navigateTo({ url: '../order/order' })
      });
    },

    /**
     * 获取数据
     */
    car_onRetrieve: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount
      })
    }
  }

})